package com.sk.rpcAssist.io.protocol;

import com.sk.rpcAssist.domain.ProvideInfoValue;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;

/**
 * description：序列化协议管理(饿汉单例)
 *
 * @author 孙文斌
 * @Date: 2021/2/7 9:54
 */
@Data
public class ProtocolManage {

    private final ConcurrentHashMap<String, MessageProtocolInter> messageProtocolMap = new ConcurrentHashMap();

    private static ProtocolManage protocolManage = new ProtocolManage();

    public static ProtocolManage getInstance() {return ProtocolManage.protocolManage;}

    private ProtocolManage() {loadPresetProtocol();}

    private final void loadPresetProtocol() {
        //加载所有预置的序列化协议
        messageProtocolMap.put(DefaultMessageProtocol.PROTOCOLCODE, DefaultMessageProtocol.getInstance());
        messageProtocolMap.put(HessianMessageProtocol.PROTOCOLCODE, HessianMessageProtocol.getInstance());
    }

    public final MessageProtocolInter getProtocolByCode(String code) {
        return messageProtocolMap.get(code);
    }

    public final MessageProtocolInter getDefaultProtocol() {
        return messageProtocolMap.get(HessianMessageProtocol.PROTOCOLCODE);
    }

    public void add(String code, MessageProtocolInter protocol) {
        //code重复检查
        MessageProtocolInter proto = messageProtocolMap.get(code);
        if(proto!=null){
            throw  new RuntimeException("protocol already exsit");
        }
        messageProtocolMap.put(code,protocol);
    }
}
